package com.atguigu.ucenterservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.ucenterservice.entity.*;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author fxy
 * @since 2022-05-11
 */
//@CrossOrigin
@RestController
@RequestMapping("/ucenterservice/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("/login")
    public R loginUser(@RequestBody LoginVo loginVo){
        //调用service方法进行登录
        //返回token 使用jwt生成
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("/register")
    public R  register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }
    //用户修改
    @PostMapping("/save")
    public R save (@RequestBody RegisterSaveVo registerSaveVo){
        ucenterMemberService.saveVo(registerSaveVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库  ，根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据id查询用户信息
    @GetMapping("/getComent/{id}")
    public CommentVo getComent(@PathVariable String id){
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        CommentVo commentVo = new CommentVo();
        commentVo.setMemberId(ucenterMember.getId());
        commentVo.setNickname(ucenterMember.getNickname());
        commentVo.setAvatar(ucenterMember.getAvatar());
        commentVo.setAge(ucenterMember.getAge());
        commentVo.setSex(ucenterMember.getSex());
        commentVo.setMobile(ucenterMember.getMobile());
        return commentVo;
    }

    //根据id查询用户信息（订单） 在common建一个UcenterMember对象
    @GetMapping("/getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        //把ucenterMember对象的值赋值给UcenterMemberOrder对象

        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(ucenterMember,ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    //查询某一天注册人数
    @GetMapping("/countRegister/{day}")
    public Integer countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegister(day);

        return count;
    }

}

